﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using SHFST.Core;

namespace SHFST.Agents
{
   class Agent
   {
      public Animation sprite;
      public Point position;
      public int life;

      public virtual void Load (Texture2D newTexture, Point newFrameSize, Point newSheetSize, Point newPos)
      {
         sprite = new Animation ();
         sprite.Load (newTexture, newFrameSize, newSheetSize);
         position = newPos;
      }

      public virtual void Update (int time)
      {
         sprite.Update (time);
      }

      public virtual void Draw (SpriteBatch batch)
      {
         sprite.Draw (batch, position);
      }

      public virtual bool Collision (Agent other)
      {
         if (other.position.X <= position.X + sprite.frameSize.X &&
             position.X <= other.position.X + other.sprite.frameSize.X &&
             other.position.Y <= position.Y + sprite.frameSize.Y &&
             position.Y <= other.position.Y + other.sprite.frameSize.Y)
         {
            Point start;
            Point end;
            if (other.position.X > position.X)
            {
               start.X = other.position.X;
            }
            else
            {
               start.X = position.X;
            }
            if (position.X + sprite.frameSize.X < other.position.X + other.sprite.frameSize.X)
            {
               end.X = position.X + sprite.frameSize.X;
            }
            else
            {
               end.X = other.position.X + other.sprite.frameSize.X;
            }
            if (other.position.Y > position.Y)
            {
               start.Y = other.position.Y;
            }
            else
            {
               start.Y = position.Y;
            }
            if (position.Y + sprite.frameSize.X < other.position.Y + other.sprite.frameSize.Y)
            {
               end.Y = position.Y + sprite.frameSize.Y;
            }
            else
            {
               end.Y = other.position.Y + other.sprite.frameSize.Y;
            }

            Color [] spriteColor = new Color [sprite.frameSize.X * sprite.frameSize.Y];
            sprite.GetData (spriteColor);
            Color [] otherColor = new Color [other.sprite.frameSize.X * other.sprite.frameSize.Y];
            other.sprite.GetData (otherColor);
            Point colorCheck;
            Point otherCheck;
            Color alpha = new Color (0, 0, 0, 0);
            for (int x = start.X; x < end.X; ++x)
            {
               colorCheck.X = x - position.X;
               otherCheck.X = x - other.position.X;
               for (int y = start.Y; y < end.Y; ++y)
               {
                  colorCheck.Y = y - position.Y;
                  otherCheck.Y = y - other.position.Y;
                  int spriteIndex = sprite.frameSize.X * colorCheck.Y + colorCheck.X;
                  int otherIndex = other.sprite.frameSize.X * otherCheck.Y + otherCheck.X;
                  if (spriteIndex < spriteColor.Length && otherIndex < otherColor.Length &&
                      spriteColor[spriteIndex] != alpha && otherColor[otherIndex] != alpha)
                  {
                     return true;
                  }
               }
            }
         }

         return false;
      }
   }
}
